package com.mgg.payment.controller.account;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;

import com.mgg.common.client.user.UserClient;
import com.mgg.common.entity.UserDO;
import com.mgg.common.enums.ResultEnum;
import com.mgg.common.util.JsonUtils;
import com.mgg.common.util.ResultVOUtil;
import com.mgg.common.vo.AccountVO;
import com.mgg.payment.enums.PaymentResultEnum;
import com.mgg.payment.service.account.AccountService;
import com.mgg.payment.vo.BankVO;
import com.mgg.payment.vo.WithdrawalsRecordVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/account")
public class AccountController {

	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
    private AccountService accountService;
    
	@Autowired
	private UserClient userClient;
	
	private final static String[] banAccountArray = {"104279", "106979", "107031", "AA0011", "107354"};
	
	/**
	 * 余额管理
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value="/info")
	public Object account(@RequestHeader("user_id") String userId) {
		//累计收益，待收收益，可提现收益
		AccountVO account = accountService.getAccountByUserId(userId);
		return ResultVOUtil.success(account);
	}
	
	/**
	 * 提现记录
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value="/details")
	public Object accountDetailsList(@RequestHeader("user_id") String userId, @RequestParam String type, @PageableDefault(page = 1, size = 20) Pageable page) {
		if ("1".equals(type)) {
			return ResultVOUtil.success(accountService.listAccountAmountDetail(userId,page));
		} else if ("2".equals(type)) {
			return ResultVOUtil.success(accountService.listAccountPreAmountDetail(userId,page));
		} else {
			return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
		}
	}
	
	/**
	 * 提现银行列表
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value="/bank/list")
	public Object getBankList() {
		List<BankVO> bankList = accountService.listBank();
		return ResultVOUtil.success(bankList);
	}
	
	/**
	 * 提现
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value="/withdrawals")
	public Object withdrawals(@RequestHeader("user_id") String userId, @RequestParam String amount, @RequestParam String type, @RequestParam String account, @RequestParam(required = false) String bank){
//		是否实名认证
		UserDO user = userClient.getUserInfo(userId);
		if (user == null) {
            return ResultVOUtil.error(ResultEnum.SYSTEM_ERROR);
		}
		if (Arrays.binarySearch(banAccountArray, user.getShopCode()) > 0) {
            return ResultVOUtil.error(PaymentResultEnum.WITHDRAWALS_FAIL.getCode(),
					MessageFormat.format(PaymentResultEnum.WITHDRAWALS_FAIL.getMessage(), "该账号禁止提现"));
		}
		
		if (StringUtils.isEmpty(user.getRealName())) {
            return ResultVOUtil.error(PaymentResultEnum.WITHDRAWALS_FAIL.getCode(),
					MessageFormat.format(PaymentResultEnum.WITHDRAWALS_FAIL.getMessage(), "请实名认证"));
		}
		
//		金额 10-1000
		BigDecimal amountBigDecimal = new BigDecimal(amount);
		if (amountBigDecimal.compareTo(new BigDecimal(10)) < 0) {
            return ResultVOUtil.error(PaymentResultEnum.WITHDRAWALS_FAIL.getCode(),
					MessageFormat.format(PaymentResultEnum.WITHDRAWALS_FAIL.getMessage(), "提现金额错误，需大于10元"));
		}
		
		AccountVO accountVO = accountService.getAccountByUserId(userId);
		if (amountBigDecimal.compareTo(accountVO.getAmount()) > 0) {
            return ResultVOUtil.error(PaymentResultEnum.WITHDRAWALS_FAIL.getCode(),
					MessageFormat.format(PaymentResultEnum.WITHDRAWALS_FAIL.getMessage(), "账户余额不足"));
		}
		
		accountService.insertWithdrawals(userId, amountBigDecimal, type, user.getRealName(), account, bank);
		return ResultVOUtil.success();
	}
	
	/**
	 * 提现记录
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value="/withdrawals/record")
	public Object withdrawalsRecord(@RequestHeader("user_id") String userId, @PageableDefault(page = 1, size = 10) Pageable page) {
		//提现时间，金额，提现到哪
        List<WithdrawalsRecordVO> list = accountService.listWithdrawalsRecord(userId,page);
		return ResultVOUtil.success(list);
	}
}
